# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 服务端方法2：WebSocket.py
# @Author: dongguangwen
# @Date  : 2025-01-05 10:07
import asyncio
import websockets


async def handler(websocket, path):
    while True:
        try:
            message = await websocket.recv()
            print(f"Received: {message}")
            await websocket.send(f"Echo: {message}")
        except websockets.exceptions.ConnectionClosed:
            print("Connection closed")
            break


start_server = websockets.serve(handler, "localhost", 6789)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
